package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

//goroutine
func hello(i int) {
	fmt.Println("hello", i)
}

//rand
func f1() {
	rand.Seed(time.Now().UnixNano())
	for i := 0; i <= 5; i++ {
		r1 := rand.Int()    //int64
		r2 := rand.Intn(10) //
		fmt.Println(r1, r2)
	}
}

var wg sync.WaitGroup

func f2(i int) {
	defer wg.Done()
	time.Sleep(time.Second * time.Duration(rand.Intn(3)))
	fmt.Println(i)
}

func main() {
	// for i := 0; i < 100; i++ {
	// 	// go hello(i) //开启一个单独的goroutine运行hello
	// 	go func(i int) { //如果没有参数是闭包，会重复值
	// 		fmt.Println(i)
	// 	}(i)
	// }
	// fmt.Println("main")
	// time.Sleep(time.Second)
	//验证rand
	//f1()
	//验证WaitGroup
	for i := 0; i < 5; i++ {
		wg.Add(1)
		f2(i)
	}
	wg.Wait()
}
